﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Attendences.Framework.BusinessLogic;
using System.Data;
using System.Text;
using Wicresoft.Common;
using System.IO;

public partial class Page_ArrangementExport : BaseAuthenticationPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
        txtStartDate.Attributes.Add("readonly", "readonly");
    }
    protected override void OnInit(EventArgs e)
    {
        pgList.PageChanged += new PageChangeEventHandler(pgList_PageChanged);
        base.OnInit(e);
    }

    void pgList_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }
    private void BindData()
    {
        string WhereClause="1=1";
        if (txtStartDate.Text != string.Empty)
        {
            WhereClause += string.Format(" and AttendDate >='{0}'", txtStartDate.Text);
        }
        if (txtEndDate.Text != string.Empty)
        {
            WhereClause += string.Format(" and AttendDate <='{0}'", txtEndDate.Text);
        }
        int count = 0;
        if (!IsPostBack)
        {
        }
        DataTable dt = DataAccess.Select(typeof(Attendences.Framework.Entity.AttendenceItem), pgList.PageIndex, pgList.PageSize, WhereClause, "AttendDate DESC", ref count);
        pgList.RecordCount = count;
        if (dt != null)
        {
            dt.Columns.Add("Users", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                Guid AttendenceItemID = new Guid(dr["ID"].ToString());
                DataTable dtUser = BLAttendences.GetAttendenceUser(string.Format("AttendenceItemID='{0}'", AttendenceItemID));
                StringBuilder sb = new StringBuilder();
                foreach (DataRow drUser in dtUser.Rows)
                {
                    sb.Append(drUser["UserDisplayName"].ToString() + "，");
                }
                if (sb.ToString() != string.Empty)
                {
                    dr["Users"] = sb.ToString().Substring(0, sb.ToString().LastIndexOf("，"));
                }
            }
        }
        ViewState["dtData"] = dt;
        rptInfo.DataSource = dt;
        rptInfo.DataBind();
    }
    protected void lbtnExport_Click(object sender, EventArgs e)
    {
        BindData();
        Response.Clear();
        string filename = "值班时间统计";
        if (txtStartDate.Text != string.Empty)
        {
            filename += Convert.ToDateTime(txtStartDate.Text).ToString("(yyyy年MM月dd日");
        }
        else
        {
            filename += "(             ";
        }
        if (txtEndDate.Text != string.Empty)
        {
            filename += "-" + Convert.ToDateTime(txtEndDate.Text).ToString("yyyy年MM月dd日)");
        }
        else
        {
            filename += "-             )";
        }
        StringBuilder sw = new StringBuilder();
       
        if (ViewState["dtData"] != null)
        {
            DataTable dtData = (DataTable)ViewState["dtData"];
            if (dtData != null && dtData.Rows.Count > 0)
            {
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(filename) + ".xls");
                Response.ContentType = "application/ms-excel";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
             
                sw.Append(filename);
                sw.Append("日期\t时间\t值班人员");
                foreach (DataRow dr in dtData.Rows)
                {
                    sw.AppendLine(Convert.ToDateTime(dr["AttendTimeStart"]).ToString("yyyy年MM月dd日 HH时mm分") + "\t" + Convert.ToDateTime(dr["AttendTimeEnd"]).ToString("yyyy年MM月dd日 HH时mm分") + "\t" + dr["Users"].ToString());
                }

                byte[] bs = System.Text.Encoding.GetEncoding("utf-8").GetBytes(sw.ToString());

                byte[] outer = new byte[bs.Length + 3];
                outer[0] = (byte)0xef;
                outer[1] = (byte)0xbb;
                outer[2] = (byte)0xbf;
                Array.Copy(bs, 0, outer, 3, bs.Length);
                Response.BinaryWrite(outer);
                Response.Flush();
                Response.End();
            }
        }
    }
    protected void lbtnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }
}
